package com.mijie.homi.listener;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mijie.homi.common.constant.ActionCode;
import com.mijie.homi.common.domain.Integration;
import com.mijie.homi.service.common.MixService;
import com.mijie.homi.service.user.UserExtInfoService;

@Service
public class IntegrationHandler {

	@Autowired
	private MixService mixService;
	@Autowired
	private UserExtInfoService userExtInfoService;
	
	private Logger logger = Logger.getLogger(IntegrationHandler.class);
	
	public void login(int userId){
		handle(userId,ActionCode.LOGIN);
	}
	
	public void postMood(int userId){
		handle(userId,ActionCode.POST_MOOD);
	}
	
	public void shareMoodStory(int userId){
		handle(userId,ActionCode.SHARE_MOOD_STORY);
	}
	
	public void uploadPhoto(int userId){
		handle(userId,ActionCode.UPLOAD_PHOTO);
	}
	
	public void settingRegion(int userId){
		handle(userId,ActionCode.SETTING_REGION);
	}
	
	public void clickAd(int userId){
		handle(userId,ActionCode.CLICK_AD);
	}
	
	public void settinName(int userId){
		handle(userId,ActionCode.SETTING_NAME);
	}
	
	public void settingPhone(int userId){
		handle(userId,ActionCode.SETTING_PHONE);
	}
	
	public void downloadAd(int userId){
		handle(userId,ActionCode.DOWNLOAD_AD);
	}
	
	public void settingVartar(int userId){
		handle(userId,ActionCode.SETTING_AVATAR);
	}
	
	public void emailVerifySuccess(int userId){
		handle(userId,ActionCode.EMAIL_VERIFY);
	}
	
	public void bindThridPart(int userId){
		handle(userId,ActionCode.BIND_THIRDPART);
	}
	
	public void shareToThirdPart(int userId){
		handle(userId,ActionCode.SHARE_TO_THIRDPART);
	}
	
	
	private void handle(int userId,int actionId){
		Integration integration = mixService.getIntegrationById(actionId);
		if(integration==null) return;
		if(integration.getDisposable()==1){
			if(!mixService.haveReward(userId,actionId)){
				mixService.updateIntegralRewardDetail(userId, actionId, integration.getIntegral());
				userExtInfoService.increIntegral(userId, integration.getIntegral());
			}
		}else{
			Integer integral = mixService.getTodayIntegralReward(userId, actionId);
			if(integral ==null || integral<integration.getPerDayLimit()){
				if(integral==null) integral = 0;
				mixService.updateIntegralRewardDetail(userId, actionId, integral+integration.getIntegral());
				userExtInfoService.increIntegral(userId, integration.getIntegral());
			}
		}
	}
}
